﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using vJine.Core.IO.Bin;
using vJine.ZMQ;

namespace vJine.ZMQ.UT
{
    public partial class frmDemo : Form
    {
        public frmDemo()
        {
            InitializeComponent();
        }

        ZmqContext context = new ZmqContext();
        private void frmDemo_Load(object sender, EventArgs e)
        {
            this.Text = "ZMQ:" + ZmqContext.Version;

            this.context.io_threads = 10;
            int io_threads = this.context.io_threads;
            this.context.max_sockets = 100;
            int sockets_max = this.context.max_sockets;
        }

        private void btnREQ_REP_Click(object sender, EventArgs e)
        {
            ZmqSocket zmqServer =
                this.context.Bind(ZmqRole.REP, "tcp://*:4040");
            
            ZmqSocket zmqClient = this.context
                .Connect(ZmqRole.REQ, "tcp://127.0.0.1:4040");

            zmqClient.Send(new byte[] { 1, 2, 3 }, true);
            zmqClient.Send(new byte[] { 4, 5 }, true);
            zmqClient.Send(new byte[] { 6,7 }, false);

            //byte[] B = new byte[4];
            //bool more = false;
            //int len = zmqServer.Receive(B, out more);
            //len = zmqServer.Receive(B, out more);

            byte[] B = new byte[10];
            zmqServer.Receive(B);

            string last_endpoint = zmqServer.last_endpoint;
        }

        private void btnPUB_SUB_Click(object sender, EventArgs e)
        {
            BizEntity X = new BizEntity() { X = "Hello World,你好世界", I = 2015 };

            ZmqSocket zmqServer =
                this.context.Bind(ZmqRole.PUB, "tcp://*:4041");
            ZmqSocket zmqClient = this.context
                .Connect(ZmqRole.SUB, "tcp://127.0.0.1:4041");

            zmqServer.Send("广播：找人");
            string msg = zmqClient.Receive<string>();

            zmqServer.Send<BizEntity>(X);
            BizEntity r = zmqClient.Receive<BizEntity>();

            X.I += 1; X.X += ";哼哼";
            zmqServer.Send<BizEntity>(X);
            zmqClient.Receive<BizEntity>(r);
        }

        [Serializable]
        public class BizEntity {
            [XmlAttribute]
            public string X { get; set; }
            [XmlAttribute]
            public int I { get; set; }
        }

        private void btnPUSH_PULL_Click(object sender, EventArgs e)
        {
            ZmqSocket zmqServer =
                this.context.Bind(ZmqRole.PUSH, "tcp://*:4042");

            ZmqSocket zmqClient = this.context
                .Connect(ZmqRole.PULL, "tcp://127.0.0.1:4042");

            zmqServer.Send("简体中文");

            string msg = zmqClient.Receive<string>();
        }

        private void btnPAIR_Click(object sender, EventArgs e)
        {
            ZmqSocket zmqServer =
                this.context.Bind(ZmqRole.PAIR, "tcp://*:4043");

            ZmqSocket zmqClient = this.context
                .Connect(ZmqRole.PAIR, "tcp://127.0.0.1:4043");

            zmqServer.Send("简体中文");

            string msg = zmqClient.Receive<string>();
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            ZmqSocket zmqSocket = this.context.Bind(ZmqRole.PUB, "tcp://*:1234");
            int send_tmo = zmqSocket.send_tmo;
            zmqSocket.send_tmo = 1234;
            send_tmo = zmqSocket.send_tmo;

            int recv_tmo = zmqSocket.recv_tmo;
            zmqSocket.recv_tmo = 4321;
            recv_tmo = zmqSocket.recv_tmo;

            string last_point = zmqSocket.last_endpoint;
        }
    }
}
